summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/time/standard_user_system_clock_core.cpp
blob: b033757ed60890a8d70dc2f1c66c475b602aebd0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/assert.h"
#include "core/core.h"
#include "core/hle/kernel/k_event.h"
#include "core/hle/service/time/standard_local_system_clock_core.h"
#include "core/hle/service/time/standard_network_system_clock_core.h"
#include "core/hle/service/time/standard_user_system_clock_core.h"

namespace Service::Time::Clock {

StandardUserSystemClockCore::StandardUserSystemClockCore(
    StandardLocalSystemClockCore& local_system_clock_core_,
    StandardNetworkSystemClockCore& network_system_clock_core_, Core::System& system_)
    : SystemClockCore(local_system_clock_core_.GetSteadyClockCore()),
      local_system_clock_core{local_system_clock_core_},
      network_system_clock_core{network_system_clock_core_},
      auto_correction_time{SteadyClockTimePoint::GetRandom()}, service_context{
                                                                   system_,
                                                                   "StandardUserSystemClockCore"} {
    auto_correction_event =
        service_context.CreateEvent("StandardUserSystemClockCore:AutoCorrectionEvent");
}

StandardUserSystemClockCore::~StandardUserSystemClockCore() {
    service_context.CloseEvent(auto_correction_event);
}

Result StandardUserSystemClockCore::SetAutomaticCorrectionEnabled(Core::System& system,
                                                                  bool value) {
    if (const Result result{ApplyAutomaticCorrection(system, value)}; result != ResultSuccess) {
        return result;
    }

    auto_correction_enabled = value;

    return ResultSuccess;
}

Result StandardUserSystemClockCore::GetClockContext(Core::System& system,
                                                    SystemClockContext& ctx) const {
    if (const Result result{ApplyAutomaticCorrection(system, false)}; result != ResultSuccess) {
        return result;
    }

    return local_system_clock_core.GetClockContext(system, ctx);
}

Result StandardUserSystemClockCore::Flush(const SystemClockContext&) {
    UNIMPLEMENTED();
    return ERROR_NOT_IMPLEMENTED;
}

Result StandardUserSystemClockCore::SetClockContext(const SystemClockContext&) {
    UNIMPLEMENTED();
    return ERROR_NOT_IMPLEMENTED;
}

Result StandardUserSystemClockCore::ApplyAutomaticCorrection(Core::System& system,
                                                             bool value) const {
    if (auto_correction_enabled == value) {
        return ResultSuccess;
    }

    if (!network_system_clock_core.IsClockSetup(system)) {
        return ERROR_UNINITIALIZED_CLOCK;
    }

    SystemClockContext ctx{};
    if (const Result result{network_system_clock_core.GetClockContext(system, ctx)};
        result != ResultSuccess) {
        return result;
    }

    local_system_clock_core.SetClockContext(ctx);

    return ResultSuccess;
}

} // namespace Service::Time::Clock